package org.example;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * ClassName: WuDaAspect
 * Package: org.example
 * Description: 演示重用切入点表达式
 *
 * @Author wanghao
 * @Create 2024-07-08 9:59
 * @Version 1.0
 */
//@Component
@Aspect
public class WuDaAspect4 {


//    @Pointcut("execution(* *Bing(..))")
//    private void myPointCut() {}

    // 1. 获取连接点信息
    @Before(value = "org.example.MyPointCut.myPointCut()")
    public void song(JoinPoint joinPoint) {
        // joinPoint 称之为连接点

        // 1.获取方法签名
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName());
        System.out.println(signature.getModifiers());
        System.out.println(signature.getDeclaringType().getName());

        // 2.获取目标方法的实际参数
        Arrays.asList(joinPoint.getArgs()).forEach(System.out::println);
    }

    // 2.获取目标方法的返回值信息
    @AfterReturning(value = "org.example.MyPointCut.myPointCut()", returning = "number")
    public void song2(Integer number) {
        System.out.println("目标方法的返回值是 " + number);
    }

    // 3.获取目标方法的异常信息
    @AfterThrowing(value = "org.example.MyPointCut.myPointCut()", throwing = "throwable")
    public void song3(Throwable throwable) {
        System.out.println("目标方法抛出了异常 " + throwable);
        System.out.println("没事");
    }

}
